﻿using System;
using System.Collections.Generic;
using System.Linq;
using FlightToolkit.SimProp;

namespace Merge
{
    public static class Merge
    {
        public enum Option
        {
            NoDuplicates,
            FirstDuplicateWins,
            LastDuplicateWins,
            AllowEqualDuplicates
        }

        public static void MergeDocumentsToSPB(string symbolPath, IEnumerable<string> inputPaths, string outputPath, Option option)
        {
            var symbols = Symbols.Load(symbolPath);

            var documents = inputPaths.Select(x => PropertySet.Load(symbols, x));

            PropertySet mergedDoc = null;
            foreach (var doc in documents)
            {
                if (mergedDoc == null)
                {
                    mergedDoc = doc;
                    continue;
                }

                switch (option)
                {
                    case Option.LastDuplicateWins:
                        foreach (var set in doc.ChildSets)
                        {
                            mergedDoc.AddChildSet(set);
                        }

                        foreach (var prop in doc.ChildProperties)
                        {
                            mergedDoc.AddChildProperty(prop);
                        }
                        break;

                    default:
                        throw new NotImplementedException();
                }
            }

            mergedDoc.Save(outputPath, Format.FlightBinary);
        }
    }
}
